package com.llkj.qingnianguoxue.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.SearchResultAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.SearchResultBean;
import com.llkj.qingnianguoxue.course.CourseDetailActivity;
import com.llkj.qingnianguoxue.course.TeacherDetailActivity;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.music.MusicDetailActivity;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.ToastUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

public class SearchResultActivity extends BaseActivity implements View.OnClickListener, BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView lvResult;//结果
    private List<SearchResultBean.ResultBean> resultData;
    private SearchResultAdapter resultAdapter;
    private String mKeyword,mSearchCategory;
    private int totalSize, mPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);

        Intent intent = getIntent();
        mKeyword = intent.getStringExtra("searchWord");
        mSearchCategory = intent.getStringExtra("searchCategory");
        setTitle(mKeyword, true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1, -1);

        initView();
        initListener();

        loadWaitDialog();
        onRefresh();
    }

    private void initView() {
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvResult = (RecyclerView) findViewById(R.id.recyclerView);
        lvResult.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        resultData = new ArrayList<>();
        resultAdapter = new SearchResultAdapter(R.layout.item_search_result, resultData);
        lvResult.setAdapter(resultAdapter);
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(this);
        resultAdapter.setOnLoadMoreListener(this);
        lvResult.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                SearchResultBean.ResultBean item = resultData.get(i);
                if("courseSearch".equals(mSearchCategory)){
                    Bundle bundle = new Bundle();
                    bundle.putString("title",item.getName());
                    bundle.putString("id",item.getId());
                    startActivity(CourseDetailActivity.class,bundle);
                }else if("musicSearch".equals(mSearchCategory)){
                    Bundle bundle  = new Bundle();
                    bundle.putString("musicId",item.getId());
                    startActivity(MusicDetailActivity.class,bundle);
                }else if("teacherSearch".equals(mSearchCategory)){
                    Bundle bundle = new Bundle();
                    bundle.putString("id",item.getId());
                    bundle.putString("name",item.getName());
                    startActivity(TeacherDetailActivity.class,bundle);
                }else {
                    if ("1".equals(item.getType()) ){//视频
                        Bundle bundle = new Bundle();
                        bundle.putString("id",item.getContent_id());
                        startActivity(VideoDetailActivity.class, bundle);
                    }else{//图书
                        Bundle bundle = new Bundle();
                        bundle.putString("id", item.getContent_id());
                        startActivity(BookDetailActivity.class, bundle);
                    }
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel://取消
                finish();
                break;
        }
    }

    private void getSearchResult() {
        if("courseSearch".equals(mSearchCategory)||"teacherSearch".equals(mSearchCategory)){
            HttpMethod.curriculumSearch(this, this, mKeyword, String.valueOf(mPage));
        }else if("musicSearch".equals(mSearchCategory)){
            HttpMethod.kojiSearch(this, this, mKeyword, String.valueOf(mPage));
        }else {
            HttpMethod.searchResult(this, this, mKeyword, String.valueOf(mPage));
        }
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_INDEX_PUT_SEARCH || method == HttpMethod.HTTP_KOJI_KOJI_SEARCH || method == HttpMethod.HTTP_CURRICULUM_CURR_SEARCH) {
            SearchResultBean resultBean = GsonUtil.GsonToBean(data, SearchResultBean.class);
            if (resultBean != null) {
                totalSize = Integer.parseInt(resultBean.getNum());
                if (resultBean.getList().size()==0){
                    ToastUtil.makeShortText(this,"没有找到相应内容");
                }
                if (mPage == 1) {
                    resultData.clear();
                    LoadDataHelper.refreshComplete(resultAdapter, resultData, refreshLayout, lvResult);
                } else {
                    resultAdapter.setEnableLoadMore(true);
                    resultAdapter.loadMoreComplete();
                }
                resultData.addAll(resultBean.getList());
                resultAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void onLoadMore() {
        mPage++;
        getSearchResult();
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        getSearchResult();
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, resultAdapter, null, lvResult, totalSize);
    }
}
